package com.proj.hot.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.adapter.TubatuAdapter;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.widget.ClipViewPager;
import com.proj.hot.widget.CommonGallery;
import com.proj.hot.widget.ScalePageTransformer;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 电影-新版
 * Created by zangchenggang on 17/8/13.
 */
public class MovieGalleryFragment extends BaseFragment {

    private CommonGallery commonGallery;
    private ImageView focus;

    private int[] imageIds = new int[]{
            R.drawable.timg1, R.drawable.timg2, R.drawable.timg3, R.drawable.timg4, R.drawable.timg5, R.drawable.timg6,
            R.drawable.timg7, R.drawable.timg8, R.drawable.timg9, R.drawable.timg10, R.drawable.timg11, R.drawable.timg12,
            R.drawable.timg13, R.drawable.timg14, R.drawable.timg15, R.drawable.timg16, R.drawable.timg17, R.drawable.timg18,
            R.drawable.timg19, R.drawable.timg20, R.drawable.timg21};


    JSONObject jsonObject;
    JSONArray jsonArray = new JSONArray();

    ClipViewPager viewPager;


    public String code;
    List<JSONObject> jsList = new ArrayList<JSONObject>();


    private TubatuAdapter movAdapter;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                DebugUtil.i("MovieGalleryFragment", "电影fragment执行了handle = 0");
                setView();
            } else if (msg.what == 1) {

            } else if (msg.what == 2) {

            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_gallery_movie, container, false);

        return rootView;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewPager = (ClipViewPager) getActivity().findViewById(R.id.movie_viewpager);
        getData();
        viewPager.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {

                DebugUtil.i("MovieGalleryFragment", "点击了 OK键");
                return false;
            }
        });

    }



    private void getData() {
        try {


            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("code", code);
            body.put("sn", GlobalParams.SN);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "featuredFirst/getDetails");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("MovieGalleryFragment", "date=" + s);
                        JSONObject jData = new JSONObject(s);
                        jsonObject = jData.getJSONObject("respbody");
                        jsonArray = jsonObject.getJSONArray("data");
                        jsList.clear();

                        for (int i = 0; i < jsonArray.length(); i++) {
                            jsList.add(jsonArray.getJSONObject(i));

                        }
                        handler.sendEmptyMessage(0);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setCodeAndInitData(String code) {
        this.code = code;

    }

    private void setView() {
        viewPager.setPageTransformer(true, new ScalePageTransformer());
        movAdapter = new TubatuAdapter(getActivity().getApplicationContext(), jsList);
        viewPager.setAdapter(movAdapter);
        viewPager.setOffscreenPageLimit(jsList.size());
        viewPager.setCurrentItem(jsList.size()/2);
        //movAdapter.addAll(jsList);
        movAdapter.notifyDataSetChanged();
//        commonGallery.setSelection(2);
//        commonGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//
//                if (GlobalParams.IS_LOGIN) {//如果登录
//
//                    if (GlobalParams.user_paytype.equals("1")) {//包月未支付
//                        Intent intent = new Intent(getActivity().getApplicationContext(), PayActivity.class);
//                        startActivity(intent);
//                    } else {
//
//                        Intent intent = new Intent(getActivity().getApplicationContext(), MovieDetailActivity.class);
//                        try {
//                            intent.putExtra("contentid", jsList.get(i).getString("recode"));
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                        startActivity(intent);
//                    }
//
//                } else {//如果没登录
//                    Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
//                    startActivity(intent);
//                }
//
//            }
//        });

    }

}
